package handler

import (
	"context"
	"fmt"
	"math/rand"
	"regexp"
	"server/basic/global"
	"server/basic/inits"
	"server/model"
	__ "server/proto"
	"time"
)

type Server struct {
	__.UnimplementedUserServer
}

// 手机号格式的判断
func IsNum(phone string) bool {
	pattern := `^1[3-9]\d{9}$`
	reg := regexp.MustCompile(pattern)
	return reg.MatchString(phone)
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Sendsms(_ context.Context, in *__.SendsmsRequest) (*__.SendsmsResponse, error) {
	if len(in.Tel) != 11 || !IsNum(in.Tel) {
		return nil, fmt.Errorf("请填写正确的手机号")
	}
	code := rand.Intn(9000) + 1000
	inits.RedisClient.Set(context.Background(), "sendsms"+in.Tel+in.Score, code, time.Minute*6)

	return &__.SendsmsResponse{}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginRequest) (*__.LoginResponse, error) {
	if len(in.Tel) != 11 || !IsNum(in.Tel) {
		return nil, fmt.Errorf("请填写正确的手机号")
	}
	get := inits.RedisClient.Get(context.Background(), "sendsms"+in.Tel+"login")
	if get.Val() != in.Sendsms {
		return nil, fmt.Errorf("验证码错误")
	}
	err := inits.RedisClient.Del(context.Background(), "sendsms"+in.Tel+"login").Err()
	if err != nil {
		return nil, fmt.Errorf("验证码删除失败")
	}
	var find model.Users
	user, err := find.LoginUser(in.Tel)
	if err != nil {
		return nil, fmt.Errorf("系统错误")
	}
	if user.Id == 0 {
		add := model.Users{
			Tel: in.Tel,
		}
		err = global.DB.Create(&add).Error
		if err != nil {
			return nil, fmt.Errorf("用户注册失败")
		}
	}
	return &__.LoginResponse{
		Id: user.Id,
	}, nil
}
func (s *Server) UserList(_ context.Context, in *__.UserListRequest) (*__.UserListResponse, error) {
	var find []model.Users
	global.DB.Find(&find)
	var finds []*__.List
	for _, user := range find {
		finds = append(finds, &__.List{
			Tel:      user.Tel,
			Name:     user.Name,
			Img:      user.Img,
			Inclduce: user.Inclduce,
		})
	}
	return &__.UserListResponse{
		List: finds,
	}, nil
}
func (s *Server) Driver(_ context.Context, in *__.DriverRequest) (*__.DriverResponse, error) {
	var find model.Drivers
	err := global.DB.Find(&find).Error
	if err != nil {
		return nil, fmt.Errorf("查询失败")
	}
	if find.Id != 0 {
		return nil, fmt.Errorf("订单已存在")
	}
	add := model.Drivers{
		Start:  in.Start,
		End:    in.End,
		Type:   in.Type,
		Status: in.Status,
	}
	err = global.DB.Create(&add).Error
	if err != nil {
		return nil, fmt.Errorf("打车失败")
	}
	return &__.DriverResponse{
		Id: add.Id,
	}, nil
}
func (s *Server) Update(_ context.Context, in *__.UpdateRequest) (*__.UpdateResponse, error) {
	//if len(in.Tel) != 11 || !IsNum(in.Tel) {
	//	return nil, fmt.Errorf("请填写正确的手机号")
	//}
	//var find model.Users
	//err := global.DB.Where("tel = ?", in.Tel).Find(&find).Error
	//if err != nil {
	//	return nil, fmt.Errorf("查询失败")
	//}
	//if find.Id == 0 {
	//	return nil, fmt.Errorf("用户不存在")
	//}
	//if in.Newpassword == in.Oldpassword {
	//	return nil, fmt.Errorf("新密码不能与旧密码一致")
	//}
	//if in.Newpassword != in.Confirmpassword {
	//	return nil, fmt.Errorf("请保持新密码与确认密码一致")
	//}
	update := model.Users{
		Password: in.Newpassword,
	}
	global.DB.Updates(&update)
	return &__.UpdateResponse{
		Id: update.Id,
	}, nil
}
